﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Web;
using System.Web.Caching;
using System.IO;


namespace NetOpen_System.Component
{
    public sealed class WebsiteVisitConfiguration
    {
		#region 构造函数
		/// <summary>
		/// 缓存键名称
		/// </summary>
        public static readonly string CacheKey = "WebsiteVisitConfig";
		private XmlDocument XmlDoc = null;


        private WebsiteVisitConfiguration(XmlDocument doc)
		{
			XmlDoc = doc;
			LoadValuesFromConfigurationXml();
		}

		#endregion



        #region 获取配置信息
        /// <summary>
        /// 获取配置节点
        /// </summary>
        /// <param name="nodePath">XPath 表达式</param>
        public XmlNode GetConfigSection(string nodePath)
        {
            return XmlDoc.SelectSingleNode(nodePath);
        }

        /// <summary>
        /// 获取默认的配置实例
        /// </summary>
        public static WebsiteVisitConfiguration GetConfig()
        {
            WebsiteVisitConfiguration config = GlobesCache.Get(CacheKey) as WebsiteVisitConfiguration;
            if (config == null)
            {
                string path = null;
                HttpContext context = HttpContext.Current;

                if (context != null)
                    path = context.Server.MapPath(string.Format("~/{0}", cntConfigFileName));
                else
                    path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, cntConfigFileName);

                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                config = new WebsiteVisitConfiguration(doc);
                GlobesCache.Max(CacheKey, config, new CacheDependency(path));
            }
            return config;

        }



        internal void LoadValuesFromConfigurationXml()
        {
            XmlNode node = null;
            node = GetConfigSection("NetOpen_System/WebsiteVisit/SQLiteConnectings");

            #region DataSoruce
            
            XmlAttributeReader attReader = new XmlAttributeReader(node.Attributes);
            S_DataSoruce = attReader.GetString("DataSource", S_DataSoruce);

            #endregion


            #region Password
            S_PassWord = attReader.GetString("Password", "NetOpen123!");


            #endregion

            #region DateSoruce

            S_DateSource = attReader.GetString("DateSource", "day");

            #endregion

            #region IsMapPath

            S_IsMapPath = attReader.GetString("IsMapPath", "yes");

            #endregion

            #region Website

            S_Website = attReader.GetString("Website", "");

            #endregion

            #region WebCookiesName

            S_WebCookiesName = attReader.GetString("WebCookiesName", "WebCookiesName");

            #endregion

            #region WebCookiesExpires

            S_WebCookiesExpires = attReader.GetString("WebCookiesExpires", "3650");

            #endregion

            #region ExcludeUrl

            S_ExcludeUrl = attReader.GetString("ExcludeUrl", "");

            #endregion

            #region DecodeUrl

            S_DecodeUrl = attReader.GetString("DecodeUrl", "");

            #endregion

            #region TextDecoding

            S_TextDecoding = attReader.GetString("TextDecoding", "utf-8");

            #endregion

        }

        

        #endregion

        #region 配置字段


        private string S_DataSoruce;
        private string S_PassWord;
        private string S_DateSource;
        private string S_IsMapPath;
        private string S_Website;
        private string S_ExcludeUrl;
        private string S_DecodeUrl;
        private string S_TextDecoding;
        private string S_WebCookiesName;
        private string S_WebCookiesExpires;
        #endregion

        public string DataSource
        {
            get {return S_DataSoruce; }
        }

        public string PassWord
        {
            get { return S_PassWord; }
        }

        public string DateSource
        {
            get { return S_DateSource;}
        }

        public string IsMapPath
        {
            get { return S_IsMapPath; }
        }

        public string Website
        {
            get { return S_Website; }
        }

        public string WebCookiesName
        {
            get { return S_WebCookiesName; }
        }

        public string WebCookiesExpires
        {
            get { return S_WebCookiesExpires; }
        }

        public string ExcludeUrl
        {
            get { return S_ExcludeUrl; }
        }

        public string DecodeUrl
        {
            get { return S_DecodeUrl; }
        }

        public string TextDecoding
        {
            get { return S_TextDecoding; }
        }

        private const string cntConfigFileName = "OpenNet_SystemWebsiteVisit.cfg.xml";
    }
}
